package com.springboot.controller;

import com.springboot.domain.BtFile;
import com.springboot.domain.Result;
import com.springboot.dto.updto.ControllerResponse;
import com.springboot.repository.FileRepository;
import com.springboot.service.FileSystemStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;

@RequestMapping(value = "/bank")
@Controller
public class FileUploadController {

    private final FileSystemStorageService storageService;
    private FileRepository fileRepository;

    @Autowired
    public FileUploadController(FileSystemStorageService storageService, FileRepository fileRepository) {
        this.storageService = storageService;
        this.fileRepository = fileRepository;
    }

    @GetMapping("/pictures/{filename:.+}")
    @ResponseBody
    public ResponseEntity<Resource> servePicture(@PathVariable String filename) {
        Resource file = storageService.loadAsResource(filename);
        return ResponseEntity
                .ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getFilename() + "\"")
                .body(file);
    }

    @GetMapping("/files/{fileId}")
    @ResponseBody
    public ResponseEntity<Resource> serveFile(@PathVariable String fileId) throws IOException {
        BtFile orifile = fileRepository.findOne(fileId);
        Resource file = storageService.loadAsResourceFile(orifile);
        return ResponseEntity
                .ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + new String(orifile.getOriginalFileName().getBytes(), "iso-8859-1") + "\"")
                .body(file);
    }

    @PostMapping("/uploadPicture")
    @ResponseBody
    public ControllerResponse handlePictureUpload(@RequestParam("file") MultipartFile file,
                                                  Model model, HttpSession session) {
        return null;
    }

    /**
     * upload enterprise template
     *
     * @param file
     * @param session
     * @return
     */
    @PostMapping("/uploadFile/{parameterName}")
    @ResponseBody
    public Result handleFileUpload(@RequestParam("file") MultipartFile file, @PathVariable String parameterName, HttpSession session) {
        return storageService.storeFile(file, parameterName, session, true);
    }

    /**
     * upload demand template
     *
     * @param file
     * @param session
     * @return
     */
    @PostMapping("/uploadFile/demand/{parameterName}")
    @ResponseBody
    public Result handleFileUploadForDemand(@RequestParam("file") MultipartFile file, @PathVariable String parameterName, HttpSession session) {
        return storageService.storeFile(file, parameterName, session, false);
    }
}
